home *** CD-ROM | disk | FTP | other *** search
- Path: lonestar.jpl.utsa.edu!nreitzel
- From: nreitzel@lonestar.jpl.utsa.edu (Norman L. Reitzel )
- Newsgroups: comp.lang.c++
- Subject: Re: Different "new" in g++ and TC++
- Date: 14 Mar 1996 19:07:26 GMT
- Organization: University of Texas at San Antonio
- Message-ID: <4i9qpe$48c@ringer.cs.utsa.edu>
- References: <3148391A.4646@numerik.uni-kiel.de> <4i9mjg$2m1@cloner3.netcom.com>
- NNTP-Posting-Host: lonestar.jpl.utsa.edu
-
- In article <4i9mjg$2m1@cloner3.netcom.com> jlilley@ix.netcom.com (John Lilley) writes:
-
- >>A is a class with constructor A::A() and n is an
- >>integer, apparently g++ and TC++ 3.0 do different jobs
- >>on the expression "new A[n]": TC++ calls the constructor
- >>apparently for every created object, while g++ does not.
- >
- >TC++ is correct. See ARM section 5.3.3, page 61:
- >
- > "Arrays of objects... if the class has a default constructor. (12.1)
- > In that case, the default constructor will be called for each
- > element of the array"
-
- The assertion that TC++ and g++ handle this case differently is untrue.
-
- This is the program that was run as a test:
-
- #include <stdio.h>
-
- class WC {
- private:
- int goof;
- public:
- WC();
- ~WC();
- };
-
- int main( void ) {
- WC *wcp = new WC[4];
- printf("Allocated!\n");
- delete [] wcp;
- printf("Deleted!\n");
- return 0;
- }
-
- WC::WC() {
- goof = 0;
- printf("Default Constructor Called.\n");
- }
-
- WC::~WC() {
- printf("Default Destructor Called.\n");
- }
-
- Note that both the constructor and destructor are default, and
- that neither of them is inline. I didn't test other cases,
- but have no a priori reason to suspect that they will behave
- differently. Here is the result of running this test using
- emx09b running on OS/2 3.0 Fixpack 17 with bash 1.1.12:
-
- $ gcc --version
- 2.7.0
- $ gcc -static -o testit.exe testit.cc
- $ testit
-
- Default Constructor Called.
- Default Constructor Called.
- Default Constructor Called.
- Default Constructor Called.
- Allocated!
-
- Default Destructor Called.
- Default Destructor Called.
- Default Destructor Called.
- Default Destructor Called.
- Deleted!
-
- As you can see, the default constructor is called for each
- of the elements of the array, as is the destructor.
-
-
-
-
- --
- Norman L. Reitzel, Jr. | "When you live beside the graveyard,
- nreitzel@lonestar.utsa.edu | you can't cry for every funeral."
- Blue Water Ventures, dba. | Russian Proverb
-